<template>
    <div class="app-container">
        <el-form ref="searchForm" inline :model="searchForm" style="position:relative;">
            <div style="text-align: right;margin-bottom: 20px;"><el-button @click="addClick(1)" type="primary">添加顶级分类</el-button></div>
        </el-form>
        <el-table :data="tableData" style="width: 100%;margin: 20px 0px;" border row-key="id" id="treeTable">
            <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
            <el-table-column prop="name" label="分类名称" width="180"></el-table-column>
            <el-table-column prop="description" label="描述" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="sort" label="顺序值" width="80" align="center"></el-table-column>
            <el-table-column label="操作" fixed="right" align="center" width="260">
                <template slot-scope="scope">
                    <el-button style="margin-bottom: 5px;" @click.stop="editClick(scope.row)" type="primary" plain size="mini">编辑</el-button>
                    <el-button style="margin-bottom: 5px;" @click.stop="addClick(2,scope.row)" plain size="mini" v-show="scope.row.parentId == 0">添加子分类</el-button>
                    <el-button style="margin-bottom: 5px;" @click.stop="delClick(scope.row)" type="danger" plain size="mini">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination @current-change="getList" layout="total, prev, pager, next, jumper" :total="pageData.totalElements"></el-pagination>
        <!-- 弹框 -->
        <el-dialog :visible.sync="showDialog" width="800px">
            <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="left" label-width="100px" style="margin: 0px 50px;">
                <el-form-item label="分类名称：" prop="name">
                    <el-input v-model="dataForm.name" placeholder="必填项。请输入长度为1-30位由中英文数字-_.组成的分类名称" maxlength="30"></el-input>
                </el-form-item>
                <el-form-item label="描述：" prop="description">
                    <el-input v-model="dataForm.description" type="textarea" placeholder="必填项。请输入描述，长度为1-200位" maxlength="200"></el-input>
                </el-form-item>
                <el-form-item label="顺序值：" prop="sort">
                    <el-input v-model="dataForm.sort" type="number" placeholder="请输入顺序值"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="addSubmit('dataForm')" type="primary">保存</el-button>
                    <el-button @click="cancelClick('dataForm')">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
import { addServiceCategory, findServiceCategory, editServiceCategory, detailServiceCategory, delServiceCategory } from '@/api/service/service-category'
import { isNameValidate, isvalidInitNumber } from '@/utils/validate'
export default {
    name: 'ServiceCategory',
    data() {
        var NameValidate=(rule, value,callback)=>{
            if(isNameValidate(value)){
                callback()
            }else{
                callback(new Error('请输入长度为1-30位由中英文数字-_.组成的名称'))
            }
        }
        var InitNumber=(rule, value,callback)=>{
            if(value){
                if(isvalidInitNumber(value)){
                    callback()
                }else{
                    callback(new Error('请输入整数'))
                }
            }else{
                callback()
            }
        }
        return{
            nowId: '',
            showDialog: false,
            dataForm: {
                name: '',
                description: '',
                sort: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入', trigger: 'blur' },
                    {  trigger: 'blur', validator: NameValidate }
                ],
                description: [
                    { required: true, message: '请输入', trigger: 'blur' }
                ],
                sort: [
                    { trigger: 'blur', validator: InitNumber }
                ],
            },
            searchForm: {},
            nowPage: 0,
            pageSize: 10,
            imageUrl: '',
            popStatus: 0,  //0为添加，1为修改
            pageData: {},
            showPicDialog: false,
            nowPic: '',
            parentOpt: [],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children'
            },
            tableData: [],
        }
    },
    methods: {
        //默认函数
        init(){
            const _this = this;
            //获取列表信息
            findServiceCategory( _this.nowPage, _this.pageSize).then(response => {
                if(response.code == 200){
                    if(response.object){
                        _this.pageData = response.object;
                        if(response.object.content){
                            _this.tableData = response.object.content;
                        }
                    }
                }else{
                    this.$message.error(response.message);
                }
            })
        },
        //添加
        addClick(type,row){
            this.popStatus = 0;
            this.showDialog = true;
            //父级栏目id
            if(type == 1){
                this.dataForm.parentId = '0';
            }else if(type == 2){
                this.dataForm.parentId = row.id;
            }
            this.dataForm.name = '';
            this.dataForm.description = '';
            this.dataForm.sort = '';
            if(this.dataForm.id){
                delete this.dataForm.id;
            }
        },
        //提交添加
        addSubmit(formName){
            const _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    if(!_this.dataForm.parentId){  //设置默认值
                        _this.dataForm.parentId = '0';
                    }
                    //提交
                    if(_this.popStatus == 0){  //添加
                        addServiceCategory(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })

                    }else if(_this.popStatus == 1){  //修改
                        _this.dataForm.id = _this.nowId;
                        editServiceCategory(_this.dataForm).then(response => {
                            if(response.code == 200){
                                this.$message.success(response.message);
                                _this.init();
                                _this.showDialog = false;
                            }else{
                                this.$message.error(response.message);
                            }
                        })
                    }
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //删
        delClick(row){
            let _this = this;
            this.$confirm('确定删除 '+row.name+' 吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //删除
                delServiceCategory(row.id).then(response => {
                    if(response.code == 200){
                        this.$message.success(response.message);
                        _this.init();
                    }else{
                        this.$message.error(response.message);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });          
            });
        },
        //编辑
        editClick(row){
            this.popStatus = 1;
            this.nowId = row.id;
            this.dataForm.name = row.name;
            this.dataForm.description = row.description;
            this.dataForm.sort = row.sort;
            this.dataForm.parentId = row.parentId;
            this.showDialog = true;
        },
        //取消
        cancelClick(formName){
            this.showDialog = false;
            this.$refs[formName].resetFields();
        },
        //分页
        getList(val) {
            this.nowPage = val - 1;
            this.init();
        },
    },
    mounted(){
        this.init();
    }
}
</script>
<style>
    #treeTable .el-icon-arrow-right:before{
        color: #2196f3;
        font-weight: bold;
        /* content: "\E6DC"; */
        content: "\E791";
    }
    #treeTable .el-table__row,#treeTable .el-table__row.el-table__row--level-0{
        background: #e8f4ff;
    }
    #treeTable .el-table__row.el-table__row--level-1{
        background: #f0f9eb;
    }
    #treeTable .el-table__row.el-table__row--level-2{
        background: oldlace;
    }
    #treeTable tr.hover-row > td{
        background-color: initial;
    }
    #treeTable.el-table th > .cell{
        text-align: center;
    }
</style>
